from flask import Flask, make_response, jsonify, g, current_app as app,Blueprint,request
from sqlalchemy import select,insert,update,func,join
from sqlalchemy.sql import text
from eim.tools.searchtool import SearchCondition,SearchConditionList,MatchLogic,PartnerRS,GlobalSearch
from eim.tools.dbtool import DBTool
from eim import db
from eim.result.simple_result import SimpleResult


dataSetBluePrint=Blueprint("dataSetBluePrint","dataSetBluePrint",__name__,url_prefix='/dataset')

@dataSetBluePrint.route("/page/<int:pn>/<int:ps>",methods=["POST"])
def pageDataSet(pn:int,ps:int):
    """
    分页获取数据源数据

    Args:
        pn (int): 当前页码
        ps (int): 当前页面尺寸
    """
    try:
        tblname="datasets"
        params = request.json
        gs = GlobalSearch(db)
        sl = SearchConditionList()
        for pm in params:
            rs =PartnerRS.from_name(pm["RS"])
            cd = pm["CD"]
            so = SearchCondition(cd["field"],MatchLogic.from_Name(cd["logic"]),cd["value"])
            sl.appendCondition(rs,so)
        res = gs.pageTableInfo(pn,ps,tblname,[],sl)
        return SimpleResult(status='T',value=res).toDict()
    except Exception as es:
        info = str(es)
        app.logger.error(info)
        return SimpleResult(msg=info).toDict()
    
@dataSetBluePrint.route("/getUserFormList",methods=['GET'])
def getUserFormList():
    """
    获取用于自定义表单列表
    """
    try:
        gs = GlobalSearch(db)
        col=["fcode","ename","cname"]
        res = gs.getTableData("userform",col)
        return SimpleResult(status="T",value=res).toDict()
    except Exception as es:
        info = str(es)
        app.logger.error(info)
        return SimpleResult(msg=info).toDict()
    
@dataSetBluePrint.route("/getUserFormVerList/<formcode>",methods=['GET'])
def getUserFormVerList(formcode:str):
    """
    获取指定表单结构编号的版本列表

    Args:
        formcode (str): 指定表单结构的编号
    """
    try:
        gs = GlobalSearch(db)
        col=["vercode"]
        sl = SearchConditionList()
        sc = SearchCondition("ufcode",MatchLogic.from_Name('EQ'),formcode)
        sl.appendCondition(PartnerRS.from_name("AND"),sc)
        res = gs.getTableData("userformversion",col,sl)
        return SimpleResult(status="T",value=res).toDict()
    except Exception as es:
        info = str(es)
        app.logger.error(info)
        return SimpleResult(msg=info).toDict()
    
@dataSetBluePrint.route("/getFormVerFields/<formcode>/<ver>",methods=['GET'])
def getFormVerFields(formcode:str,ver:str):
    """
    获取指定数据表结构下指定版本的字段列表

    Args:
        formcode (str): 指定表结构的编码
        ver (str): 指定表结构的版本
    """
    try:
        gs = GlobalSearch(db)
        col=["fieldcode","ename","cname"]
        sl = SearchConditionList()
        sc = SearchCondition("formcode",MatchLogic.from_Name('EQ'),formcode)
        sl.appendCondition(PartnerRS.from_name("AND"),sc)
        
        sc1 = SearchCondition("ver",MatchLogic.from_Name('EQ'),ver)
        sl.appendCondition(PartnerRS.from_name("AND"),sc1)
        
        res = gs.getTableData("formfields",col,sl)
        return SimpleResult(status="T",value=res).toDict()
    except Exception as es:
        info = str(es)
        app.logger.error(info)
        return SimpleResult(msg=info).toDict()